/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-11-04 09:44:50
 * @filename : static/sea-modules/swipe/swipe.js
 * @version : 
 * @description : 
 */

;(function($){

      var box = null,
          opts= {
             swipeLeft : function(){},
             swipeRight : function(){},
             swipeUp: function(){},
             swipeDown: function(){}
          },
          bool = false,
          touch = {move:0};

       /**
        * 字面量方法对象 
        */ 
      var methods = {        

            init : function() {
                touch.move = 0;
                this.bindEvt();
            },

            bindEvt : function() {

                $(box).on('touchstart',function(e){
                    touch.timeStart = new Date().getTime();
	                touch.x1 = e.touches[0].pageX;
	                touch.y1 = e.touches[0].pageY;
                    touch.x2 = e.touches[0].pageX;
			        touch.y2 = e.touches[0].pageY;
                    bool = true;
                }).on('touchmove',function(e){
		            if (bool) {
		                touch.x2 = e.touches[0].pageX;
			            touch.y2 = e.touches[0].pageY;
                        methods.touchDirection();
			            if (touch.direction == 'x') {
                            e.preventDefault();
                            e.stopPropagation();
			            }
                    }
                }).on('touchend',function(e){
		             if (bool) {
			             bool = false;
                         var timeNum = touch.timeEnd - touch.timeStart;
			             touch.timeEnd = new Date().getTime();
                         e.preventDefault();
                         //e.stopPropagation();
                         touch.MX = touch.x2 - touch.x1;
                         touch.MY = touch.y2 - touch.y1; 

			             if (touch.direction == 'x') {                           			                
				            if (touch.x2 - touch.x1 > 30 && timeNum < 500) {
				                opts.swipeRight(e,touch);
				            } else if (touch.x2 - touch.x1 < -30 && timeNum < 500){
				                opts.swipeLeft(e,touch);
	                        } 
			             } else {
                            if (touch.y2 - touch.y1 > 30 && timeNum < 500) {
				                opts.swipeDown(e,touch);
				            } else if (touch.y2 - touch.y1 < -30 && timeNum < 500){
				                opts.swipeUp(e,touch);
	                        } 

                         }
                     }
                });      
            },

            touchDirection : function() {
		        touch.direction = 'y';
		        if (Math.abs(touch.x1 - touch.x2) > Math.abs(touch.y1 - touch.y2)) {
		             touch.direction = 'x';
		        } else {
		             touch.direction = 'y';
                }                
            }

    };

	/**
	 * slider 插件
	 */
	$.fn.swipe = function(options) {
             box = this;
             opts = $.extend({}, opts, options || {});	  	      
             methods.init();
             return $(this);
	}

    if (typeof define === "function" ) {
        define("swipe/zepto.swipe.js", [], function () { return $.fn.swipe;} );
    }

})(Zepto)
